const express = require('express');
const router = express.Router();
const db = require('../utils/db');

// 获取文章分类列表
router.get('/cates', async(req, res) => {
    let r = await db('select * from category')
    if (r && r.length > 0) {
        res.send({
            status: 0,
            message: '获取成功',
            data: r,
        })
    } else {
        res.send({
            status: 1,
            message: '获取错误',
        })
    }
});
// 新增文章分类
router.post('/addcates', async(req, res) => {
    // let name = req.body.name;
    // let alias = req.body.alias;
    let r = await db('insert into category set ?', req.body);
    if (r && r.affectedRows > 0) {
        res.send({ status: 0, message: '添加成功' })
    } else { res.send({ status: 1, message: '添加失败' }) }
});
// 根据 Id 删除文章分类
router.get('/deletecate/:id', async(req, res) => {
    let id = req.params.id;
    console.log(id)
    let r = await db('delete from category where id =?', id);
    if (r && r.affectedRows > 0) {
        res.send({
            status: 0,
            message: '删除成功',
        })
    } else {
        res.send({
            status: 1,
            message: '删除失败',
        })
    }
});
// 更新文章分类
router.post('/updatecate', async(req, res) => {

    let r = await db('update category set ? where id = ?', [req.body, req.body.Id]);
    if (r && r.affectedRows > 0) {
        res.send({ status: 0, message: '更新成功' })
    } else { res.send({ status: 1, message: '更新失败' }) }
})
module.exports = router;